package com.zxbit.flash.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;

import java.security.MessageDigest;

public class GlideImageLoader {

    public static void LoadImage(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.fitCenter().diskCacheStrategy(DiskCacheStrategy.NONE).dontAnimate();
        Glide.with(context).load(url).apply(options).into(iv);
    }

    public static void LoadImage(Context context, Uri uri, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.fitCenter().diskCacheStrategy(DiskCacheStrategy.NONE).dontAnimate();
        Glide.with(context).load(uri).apply(options).into(iv);
    }

    public static void loadIdCardImg(Context context, Bitmap bitmap, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.transform(new RotateTransformation(context,-90f));
        Glide
                .with(context)
                .load(bitmap)
                .apply(options)
                .into(iv);
    }

    public static void loadIdCardImg(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.transform(new RotateTransformation(context,-90f));
        Glide
                .with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }
    public static class RotateTransformation  extends BitmapTransformation {

        //旋转默认0
        private float rotateRotationAngle = 0f;

        public RotateTransformation(Context context ,float rotateRotationAngle)
        {
            super(context);
            this.rotateRotationAngle = rotateRotationAngle ;
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            Matrix matrix = new Matrix();
            //旋转
            matrix.postRotate(rotateRotationAngle);
            //生成新的Bitmap
            return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);

            //return null;
        }


        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        }
    }


}
